package com.spark.trouble

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.alert
import com.spark.common.ext.sdf
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.trouble.model.TroubleDealListModel
import kotlinx.android.synthetic.main.frag_deal_trouble.*

/**
 * 隐患处理
 *  * @info 处理流程状态
 *  1 -> 待处理
 *  2 -> 审核中/待审核
 *  3 -> 被驳回
 *  4 -> 确认中/待确认
 *  5 -> 整改中/待整改
 *  6 -> 验收中/待验收
 *  7 -> 验收驳回
 *  8 -> 待存储
 *  9 -> 已存储
 *  10 -> 已归档
 */
class TroubleDealFragment : BaseFragment() {
    private lateinit var troubleDealAdapter: TroubleDealAdapter
    private var currPage = 1
    private var totalPage = 1


    companion object {
        fun newInstance(): TroubleDealFragment {
            val target = TroubleDealFragment()
            val args = Bundle()
            target.arguments = args
            return target
        }
    }


    override fun contentResId(): Int {
        return R.layout.frag_deal_trouble
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_title.text = "处理隐患"
        iv_back.setOnClickListener { pop() }


        troubleDealAdapter = TroubleDealAdapter(R.layout.item_trouble_deal).apply {
            setOnItemClickListener { _, _, position ->
                val data = troubleDealAdapter.data[position]
                activity?.apply { HTHandleActivity.actionStart(this, Gson().toJson(data)) }
            }
        }
        recy_troubles.apply {
            adapter = troubleDealAdapter
            layoutManager = LinearLayoutManager(context)
        }

        fetchTroubles(currPage)
        refresh_layout.apply {
            setOnRefreshListener {
                currPage = 1
                fetchTroubles(currPage)
            }
            setOnLoadmoreListener {
                if (currPage > totalPage) {
                    alert("没有更多数据了")
                    finishLoadmore()
                } else {
                    currPage++
                    fetchTroubles(currPage)
                }
            }
        }
    }

    @SuppressLint("CheckResult")
    private fun fetchTroubles(page: Int) {
        val params = mutableMapOf(
                Pair(Const.qr_pageNum, "$page"),
                Pair(Const.qr_pageSize, "10")
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_ht_list}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, TroubleDealListModel::class.java)?.apply {
                                if (code == "200") {
                                    totalPage = data.pageCount
                                    if (page == 1)
                                        troubleDealAdapter.data.clear()
                                    troubleDealAdapter.addData(resultMaps)
                                }
                            }
                        },
                        {
                            it.printStackTrace()
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            dispose = it
                        }
                )
    }
}

class TroubleDealAdapter(layoutRes: Int) : BaseQuickAdapter<TroubleDealListModel.ResultMap, BaseViewHolder>(layoutRes) {
    private val color_30c6d3: Int = Color.parseColor("#30c6d3")
    private val color_6053fa: Int = Color.parseColor("#6053fa")
    private val color_fea337: Int = Color.parseColor("#fea337")
    private val color_4ba5fb: Int = Color.parseColor("#4ba5fb")
    private val color_fc7828: Int = Color.parseColor("#fc7828")
    private val color_fd5249: Int = Color.parseColor("#fd5249")

    private val sdf = sdf()

    override fun convert(helper: BaseViewHolder?, item: TroubleDealListModel.ResultMap?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_title, item.source_description)
            setText(R.id.tv_time, sdf.format(item.createTime))

            val tvState = getView<TextView>(R.id.tv_state)
            tvState.text = item.stateFlag
            setText(R.id.tv_task_type, when (item.taskType) {
                "1" -> "标准化检查-主任务"
                "2" -> "标准化检查-子任务"
                "3" ->"借阅申请"
                "4" -> "文件传输"
                "5" -> "归档申请"
                "6" -> "隐患-主任务"
                "7" -> "未分配任务"
                "8" -> "现场治理"
                "9" -> "限时整改"
                "10" -> "挂牌督办"
                "11" -> "隐患升级"
                "12" -> "重大隐患"
                else -> "不属于隐患 [${item.taskType}]"
            })
            when (item.stateFlag) {
                "审核中" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                        setTextColor(color_4ba5fb)
                    }
                }
                "被驳回" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                        setTextColor(color_fd5249)
                    }
                }
                "待存储" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_f7f1e8)
                        setTextColor(color_fea337)
                    }
                }
                "确认中" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                        setTextColor(color_4ba5fb)
                    }
                }
                "整改中" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_fff1e9)
                        setTextColor(color_fc7828)
                    }
                }
                "验收中" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                        setTextColor(color_4ba5fb)
                    }
                }
                "待审核" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                        setTextColor(color_6053fa)
                    }
                }
                "延期中" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                        setTextColor(color_fd5249)
                    }
                }
                "待处理" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                        setTextColor(color_6053fa)
                    }
                }
                "验收驳回" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                        setTextColor(color_fd5249)
                    }
                }
                "待同步" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                        setTextColor(color_30c6d3)
                    }
                }
                "延期申请" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                        setTextColor(color_fd5249)
                    }
                }
                "已归档" -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                        setTextColor(color_30c6d3)
                    }
                }
                else -> {
                    tvState.apply {
                        background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                        setTextColor(color_30c6d3)
                    }
                }
            }
        }
    }

}